import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/user/Users'
import Roles from '../components/right/Roles'
import Rights from '../components/right/Rights'
import Goods from '../components/goods/Goods'
import Params from '../components/goods/Params'
import Categories from '../components/goods/Categories'
import Orders from '../components/order/Orders'
import Reports from '../components/static/Reports'
import Add from '../components/goods/Add'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      redirect: '/welcome',
      component: Home,
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/roles', component: Roles },
        { path: '/rights', component: Rights },
        { path: '/goods', component: Goods },
        { path: '/params', component: Params },
        { path: '/categories', component: Categories },
        { path: '/orders', component: Orders },
        { path: '/reports', component: Reports },
        { path: '/goods/add', component: Add }
      ]
    }
  ]
})
// 为路由对象添加beforeEach导航守卫
router.beforeEach((to, from, next) => {
  // 如果用户访问的登录页，直接放行
  if (to.path === '/login') return next()
  // 从sessionStorage 中获取到的token值
  const tokenStr = window.sessionStorage.getItem('token')
  //没有token，强制跳转到登录页面
  if (!tokenStr) return next('/login')
  next()
})

export default router
